//
//  ViewController.swift
//  TouchIdDemo
//
//  Created by 博广 on 2016/12/19.
//  Copyright © 2016年 博广. All rights reserved.
//

import UIKit
import LocalAuthentication

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "首页"
        self.view.backgroundColor = UIColor.red

        self.testTouchID();
    }
    
    /**测试touchid*/
    func testTouchID() {
        let laContext = LAContext()
        if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
            print("可以使用touchId")
                laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "为了您的安全，需要验证密码", reply: { (success, error) in
                    
                    let secondVC = SecondViewController()
                    var msg:String
                    if success {
                        msg = "密码输入正确"
                        print(msg)
                    } else {
                        msg = "密码输入错误"
                        print(msg)
                    }
                    
                    secondVC.msg = msg
                    //关键，必须要在主线程，回调是异步的
                    DispatchQueue.main.async {
                        self.navigationController?.pushViewController(secondVC, animated: true)
                    }
                })
        } else {
            print("不能使用touchid")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

